fix: throw MockException when HttpClient is not mockable#367
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves error handling for HttpClient mocking by throwing a clear MockException when attempting to setup or verify an HttpClient that was not properly mocked with a mockable HttpMessageHandler.
Changes:
- Replaced fallback logic that attempted to mock
HttpClientdirectly with explicit exception throwing - Added validation to ensure
ConstructorParametersarray has elements before accessing - Added comprehensive test coverage for the error scenarios across all HTTP methods
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| Source/Mockolate/Web/HttpClientExtensions.cs | Removed unused CastToMockOrThrow helper method |
| Source/Mockolate/Web/HttpClientExtensions.Setup.Delete.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Setup.Get.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Setup.Patch.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Setup.Post.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Setup.Put.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Verify.Delete.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Verify.Get.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Verify.Patch.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Verify.Post.cs | Replaced fallback logic with exception throwing and added length check |
| Source/Mockolate/Web/HttpClientExtensions.Verify.Put.cs | Replaced fallback logic with exception throwing and added length check |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.DeleteTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.GetTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PatchTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PostTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PutTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.DeleteTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.GetTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PatchTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PostTests.cs | Added tests for string and Uri overloads without mocked handler |
| Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PutTests.cs | Added tests for string and Uri overloads without mocked handler |
🚀 Benchmark ResultsDetails
|
532255d to
aacf9f2
Compare
|
|
This is addressed in release v0.52.0. |



This PR improves error handling for
HttpClientmocking by throwing a clearMockExceptionwhen attempting to setup or verify anHttpClientthat was not properly mocked with a mockableHttpMessageHandler.Key Changes:
HttpClientdirectly with explicit exception throwingConstructorParametersarray has elements before accessing